﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Xml;
using Bitirme.Controllers;
using Bitirme.DAL.Entities;
using System.Linq;
using System.Collections.Generic;
using System.Drawing;

namespace Bitirme.Views
{
    public partial class GanttChart : System.Web.UI.Page
    {
        private Random rand = new Random();

        private static String HexConverter(System.Drawing.Color c)
        {
            return c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string id = Request.QueryString["id"];
            int projectID = Int32.Parse(id);

            Project project;
            Dictionary<int, String> colorspace = new Dictionary<int, string>();
            EmployeeHandler emhandler = new EmployeeHandler();
            AssignmentHandler ahandler = new AssignmentHandler();
            List<Employee> employeeList;
            employeeList = emhandler.GetAllEmployees();
            foreach (Employee employ in employeeList) { 
                KnownColor[] names = (KnownColor[])Enum.GetValues(typeof(KnownColor));
                KnownColor randomColorName = names[rand.Next(names.Length)];
                Color randomColor = Color.FromKnownColor(randomColorName);
                string col;
                col = HexConverter(randomColor);
                colorspace.Add(employ.ID, col);
            }

            ProjectHandler prHandler = new ProjectHandler();
            project = prHandler.GetProjectsWithAllAttributesByID(projectID);

            //Creating data of ou Task.xml programatically 
            XmlTextWriter xwriter = new XmlTextWriter(Server.MapPath("project.xml"), null); //Creating a Xml Text writer 
            xwriter.Formatting = Formatting.Indented;// Setting indendation in xml file
            xwriter.Indentation = 4;// Indentation space is 4 here

            xwriter.WriteStartElement("project");
            
            xwriter.WriteStartElement("task");

            xwriter.WriteStartElement("pID"); // WriteStartElement writes a tag 
            xwriter.WriteString(project.ID.ToString()); // WriteString  writes strin to xml file
            xwriter.WriteEndElement();// WriteStartElement writes a end tag
            xwriter.WriteStartElement("pName");
            xwriter.WriteString(project.Name);
            xwriter.WriteEndElement();

            xwriter.WriteStartElement("pStart");
            xwriter.WriteString(project.StartDate.ToString());
            xwriter.WriteEndElement();

            xwriter.WriteStartElement("pEnd");
            xwriter.WriteString(project.EndDate.ToString());
            xwriter.WriteEndElement();

            xwriter.WriteStartElement("pParent");
            xwriter.WriteString("0");
            xwriter.WriteEndElement();

            xwriter.WriteStartElement("pGroup");
            xwriter.WriteString("1");
            xwriter.WriteEndElement();

            xwriter.WriteStartElement("pOpen");
            xwriter.WriteString("1");
            xwriter.WriteEndElement();

            xwriter.WriteEndElement();
            
            var sortedQuery = from sortedassign in project.AssignmentList
                              orderby sortedassign.StartDate
                              select sortedassign;
            
            List<int> workpackageID = new List<int>();
            foreach (Assignment assigned in sortedQuery)
            {
                //Workpackage için
                /*
                if (!workpackageID.Contains(assigned.WorkPackage.ID)) {
                    xwriter.WriteStartElement("task");

                    xwriter.WriteStartElement("pID"); // WriteStartElement writes a tag 
                    xwriter.WriteString(assigned.WorkPackage.ID.ToString()); // WriteString  writes strin to xml file
                    xwriter.WriteEndElement();// WriteStartElement writes a end tag

                    xwriter.WriteStartElement("pName");
                    xwriter.WriteString(assigned.WorkPackage.Name + " <b>(" + assigned.WorkPackage.Requirement.RequirementCapability.Name + ")</b>");
                    xwriter.WriteEndElement();

                    xwriter.WriteStartElement("pGroup");
                    xwriter.WriteString("1");
                    xwriter.WriteEndElement();

                    xwriter.WriteStartElement("pParent");
                    xwriter.WriteString(project.ID.ToString());
                    xwriter.WriteEndElement();

                    xwriter.WriteStartElement("pOpen");
                    xwriter.WriteString("1");
                    xwriter.WriteEndElement();

                    string dependency = "";
                    foreach (WorkPackage wp in assigned.WorkPackage.Dependency)
                    {
                        dependency = dependency + wp.ID + ",";
                    }
                    if (dependency.Length > 1)
                        dependency = dependency.Remove(dependency.Length - 1, 1);
                    //xwriter.WriteStartElement("pDepend");
                    //xwriter.WriteString(dependency);
                    //xwriter.WriteEndElement();

                    xwriter.WriteEndElement();

                    workpackageID.Add(assigned.WorkPackage.ID);
                }
                 * */
                //Assignment için
                xwriter.WriteStartElement("task");

                xwriter.WriteStartElement("pID"); // WriteStartElement writes a tag 
                xwriter.WriteString(assigned.ID.ToString()); // WriteString  writes strin to xml file
                xwriter.WriteEndElement();// WriteStartElement writes a end tag
                
                xwriter.WriteStartElement("pName");
                xwriter.WriteString(assigned.WorkPackage.Name);
                xwriter.WriteEndElement();

                xwriter.WriteStartElement("pRes");
                xwriter.WriteString(assigned.Employee.Name+" "+assigned.Employee.SurName);
                xwriter.WriteEndElement();

                xwriter.WriteStartElement("pStart");
                xwriter.WriteString(assigned.StartDate.ToString());
                xwriter.WriteEndElement();

                xwriter.WriteStartElement("pEnd");
                xwriter.WriteString(assigned.EndDate.ToString());
                xwriter.WriteEndElement();

                xwriter.WriteStartElement("pColor");
                xwriter.WriteString(colorspace[assigned.Employee.ID]);
                xwriter.WriteEndElement();

                xwriter.WriteStartElement("pGroup");
                xwriter.WriteString("0");
                xwriter.WriteEndElement();

                xwriter.WriteStartElement("pParent");
                xwriter.WriteString(project.ID.ToString());
                xwriter.WriteEndElement();

                xwriter.WriteStartElement("pOpen");
                xwriter.WriteString("1");
                xwriter.WriteEndElement();

                string dependencyy = "";
                foreach (WorkPackage wp in assigned.WorkPackage.Dependency)
                {
                    List<Assignment> assignments = ahandler.GetAssignmentsByWorkPackageID(wp.ID);
                    foreach(Assignment asi in assignments)
                        dependencyy = dependencyy + asi.ID + ",";
                }
                if (dependencyy.Length > 1)
                    dependencyy = dependencyy.Remove(dependencyy.Length - 1, 1);
                xwriter.WriteStartElement("pDepend");
                xwriter.WriteString(dependencyy);
                xwriter.WriteEndElement();

                xwriter.WriteEndElement();
            
            }


            xwriter.WriteEndElement();
            //Start to read  and write Tasks to Xml file 
            xwriter.Flush();
            xwriter.Close(); //Close XML File
        }
    }
}